package com.xoa.service.vehicle;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.vehicle.VehicleMapper;
import com.xoa.dao.vehicle.VehicleUsageMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.department.Department;
import com.xoa.model.institution.InstitutionContentWithBLOBs;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.users.Users;
import com.xoa.model.vehicle.VehicleUsageWithBLOBs;
import com.xoa.model.vehicle.VehicleWithBLOBs;
import com.xoa.service.sms.SmsService;
import com.xoa.util.AjaxJson;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import com.xoa.util.page.PageParams;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.*;

/**
 * 创建作者:   高亚峰
 * 创建日期:   2017/9/19 15:15
 * 类介绍  :   车辆管理
 * 构造参数:
 */
@Service
public class VeHicleUsageService {

    @Autowired
    private VehicleUsageMapper vehicleUsageMapper;

    @Resource
    private SmsService smsService;

    @Resource
    private UsersMapper usersMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Resource
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Resource
    private SysCodeMapper sysCodeMapper;

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月19日 下午15:01:41
     * 方法介绍:   车辆申请
     * 参数说明:   VehicleUsageWithBLOBs
     * @return    ajaxjson
     */
    public AjaxJson addVeHicle(HttpServletRequest request, VehicleUsageWithBLOBs vehicleUsageWithBLOBs){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            //从session中获取当前登录人的信息
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            String userId = users.getUserId();
            //申请人为当前登录人
            vehicleUsageWithBLOBs.setVuProposer(userId);
            Date date1 =new Date();
            vehicleUsageWithBLOBs.setVuRequestDate(date1);

            //用车人
            String vuUser = vehicleUsageWithBLOBs.getVuUser();
            if(vuUser!=null){
                vehicleUsageWithBLOBs.setVuUser(vuUser);
            }

            //审批人
            String deptManager = vehicleUsageWithBLOBs.getDeptManager();
            if(deptManager!=null){
                vehicleUsageWithBLOBs.setDeptManager(deptManager);
            }

            synchronized (this){
                Map map=new HashedMap();
                map.put("vId",vehicleUsageWithBLOBs.getvId());
                map.put("startTime",vehicleUsageWithBLOBs.getVuStart());
                map.put("endTime",vehicleUsageWithBLOBs.getVuEnd());
                int VehicleCount = vehicleUsageMapper.getCount(map);//要是用的车辆和已申请车辆不冲突的数量
                if( VehicleCount == 0 ){
                    if(!StringUtils.checkNull(vehicleUsageWithBLOBs.getDeptManager())){
                        vehicleUsageWithBLOBs.setDmerStatus("0");
                    }
                    vehicleUsageMapper.insertSelective(vehicleUsageWithBLOBs);

                    //判断是否发送事务提醒,部门审批
                    if (!StringUtils.checkNull(vehicleUsageWithBLOBs.getSmsRemind()) && vehicleUsageWithBLOBs.getSmsRemind().equals("1")){
                        VehicleUsageReminder(request,vehicleUsageWithBLOBs,vehicleUsageWithBLOBs.getDeptManager());
                    }

                    //调度人审批
                    if (!StringUtils.checkNull(vehicleUsageWithBLOBs.getSms2Remind()) && vehicleUsageWithBLOBs.getSms2Remind().equals("1")){
                        VehicleUsageReminder( request, vehicleUsageWithBLOBs, vehicleUsageWithBLOBs.getVuOperator() );
                    }

                    ajaxJson.setFlag(true);
                    ajaxJson.setMsg("申请成功");
                }else{
                    ajaxJson.setFlag(false);
                    ajaxJson.setMsg("您预约车辆的时间段已经被占用");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setFlag(false);
            ajaxJson.setMsg(e.getMessage());
        }
        return ajaxJson;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月19日 下午15:01:41
     * 方法介绍:   车辆使用申请列表查询
     * 参数说明:   request
     * @return    ajaxjson
     */
    public AjaxJson selectAllVeHicle(HttpServletResponse response,HttpServletRequest request, String vuStatusName,String applyStartTime,VehicleUsageWithBLOBs vehicleUsageWithBLOBs3, String applyEndTime,String export){
        if(export==null){
            export="0";
        }

        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("vId",vehicleUsageWithBLOBs3.getvId());
            map.put("vuDriver",vehicleUsageWithBLOBs3.getVuDriver());
            map.put("applyStartTime",applyStartTime);
            map.put("applyEndTime",applyEndTime);
            map.put("vuUser",vehicleUsageWithBLOBs3.getVuUser());
            map.put("vuDept",vehicleUsageWithBLOBs3.getVuDept());
            map.put("vuProposer",vehicleUsageWithBLOBs3.getVuProposer());
            map.put("vuOperator",vehicleUsageWithBLOBs3.getVuOperator());
            map.put("vuReason",vehicleUsageWithBLOBs3.getVuReason());
            map.put("vuRemark",vehicleUsageWithBLOBs3.getVuRemark());
            //声明一个空的车辆集合
            List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBs1 = null;
            //待审批车辆
            if(vuStatusName.equals("0")){
                //给vehicleUsageWithBLOBs1重新赋值成为待审批的车辆
                vehicleUsageWithBLOBs1= vehicleUsageMapper.getNoApproval(map);
            }
            //点击已批准的时候
            else if(vuStatusName.equals("1")){
                //给已批准的list赋值
                vehicleUsageWithBLOBs1=vehicleUsageMapper.selectAlredyAllVeHicle();
                List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllApprovalVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs.getDeptManager()==null || vehicleUsageWithBLOBs.getDeptManager().equals("")){
                        vehicleUsageWithBLOBs1.add(vehicleUsageWithBLOBs);
                    }
                }
            }
            //点击使用中
            else if(vuStatusName.equals("2")){
                vehicleUsageWithBLOBs1=vehicleUsageMapper.selectAlredyAllVeHicle();
                List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllApprovalVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs.getDeptManager()==null){
                        vehicleUsageWithBLOBs1.add(vehicleUsageWithBLOBs);
                    }
                }
                //遍历已批准 查看时间
                List <VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList =new ArrayList<VehicleUsageWithBLOBs>();
                for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBs1){
                    Date date =new Date();
                    Date vuStart = vehicleUsageWithBLOBs.getVuStart();
                    Date vuEnd = vehicleUsageWithBLOBs.getVuEnd();
                    long time = date.getTime();
                    long startTime = vuStart.getTime();
                    long endTime = vuEnd.getTime();
                    if(startTime<=time&&endTime>=time){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs);
                    }
                }
                //给vehicleUsageWithBLOBs1重新赋值成为使用中的车辆
                vehicleUsageWithBLOBs1=vehicleUsageWithBLOBsList;
            }
            //使用结束的车辆
            else if(vuStatusName.equals("3")){
                vehicleUsageWithBLOBs1=vehicleUsageMapper.selectAlredyAllVeHicle();
                List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllApprovalVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs.getDeptManager()==null){
                        vehicleUsageWithBLOBs1.add(vehicleUsageWithBLOBs);
                    }
                }
                //遍历已批准 查看时间
                List <VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList =new ArrayList<VehicleUsageWithBLOBs>();
                for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBs1){
                    Date date =new Date();
                    Date vuStart = vehicleUsageWithBLOBs.getVuStart();
                    Date vuEnd = vehicleUsageWithBLOBs.getVuEnd();
                    long time = date.getTime();
                    long startTime = vuStart.getTime();
                    long endTime = vuEnd.getTime();
                    if(startTime>=time&&endTime>=time){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs);
                    }
                }
                //给vehicleUsageWithBLOBs1重新赋值成为使用结束的车辆
                vehicleUsageWithBLOBs1=vehicleUsageWithBLOBsList;
            }
            //点击未准车辆
            else if(vuStatusName.equals("4")){
                //给vehicleUsageWithBLOBs1重新赋值成为未批准车辆
                vehicleUsageWithBLOBs1=vehicleUsageMapper.selectRefuseVehicle(map);
            }
            else if(vuStatusName.equals("5")){
                //给vehicleUsageWithBLOBs1重新赋值成为未使用的车辆
                vehicleUsageWithBLOBs1=vehicleUsageMapper.selectNoUseVehicle(map);
            }
            else{
                vehicleUsageWithBLOBs1=vehicleUsageMapper.seVehicle(map);
            }
            if(vehicleUsageWithBLOBs1!=null){
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs:vehicleUsageWithBLOBs1){
                    //车牌号
                    if(!StringUtils.checkNull(vehicleUsageWithBLOBs.getvId())){
                        VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs.getvId()));
                        if(vehicleWithBLOBs!=null){
                            vehicleUsageWithBLOBs.setvIdNum(vehicleWithBLOBs.getvNum());
                        }else{
                            vehicleUsageWithBLOBs.setvIdNum("");
                        }
                    }
                    //用车人
                    if(vehicleUsageWithBLOBs.getVuUser()!=null && vehicleUsageWithBLOBs.getVuUser()!=""){
                        Users byuserId = usersMapper.getByUserId(vehicleUsageWithBLOBs.getVuUser().substring(0,vehicleUsageWithBLOBs.getVuUser().length()-1));
                        if(byuserId!=null){
                            vehicleUsageWithBLOBs.setVuUserName(byuserId.getUserName());
                        }
                    }

                    //部门审批人处理
                    if(vehicleUsageWithBLOBs.getDeptManager()!=null && vehicleUsageWithBLOBs.getDeptManager()!=""){
                        Users byuserId1 = usersMapper.getByUserId(vehicleUsageWithBLOBs.getDeptManager().substring(0,vehicleUsageWithBLOBs.getDeptManager().length()-1));
                        if(byuserId1!=null){
                            vehicleUsageWithBLOBs.setDeptManagerName(byuserId1.getUserName());
                        }
                    }

                    //随行人员处理
                    String vuSuite = vehicleUsageWithBLOBs.getVuSuite();
                    if(vuSuite!=null && vuSuite!=""){
                        StringBuffer sb =new StringBuffer();
                        String[] split = vuSuite.split(",");
                        for(String s :split){
                            Users byuserId2 = usersMapper.getByUserId(s);
                            if(byuserId2!=null){
                                sb.append(byuserId2.getUserName()+",");
                            }
                            vehicleUsageWithBLOBs.setVuSuiteName(sb.toString());
                        }

                    }

                }
            }
            if(export.equals("1")){
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("车辆信息导入表", 9);
                String[] secondTitles = {"车牌号", "用车人", "随行人员","事由","开始时间", "结束时间", "备注"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"vIdNum","vuUserName", "vuSuiteName","vuReason","vuStart", "vuEnd", "vuRemark"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, vehicleUsageWithBLOBs1, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "车辆信息导出表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }

            ajaxJson.setObj(vehicleUsageWithBLOBs1);
            ajaxJson.setFlag(true);
            ajaxJson.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setMsg(e.getMessage());
        }
        return ajaxJson;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月19日 下午17:01:41
     * 方法介绍:   部门审批列表展示
     * 参数说明:   request
     * @return    ajaxjson
     */
    public AjaxJson deptApproval(HttpServletRequest request,String dmerStatus,PageParams pageParams){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            //从session中获取当前登录人的信息
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            String userId = users.getUserId();
            Map<String,Object> map = new HashMap<>();
            map.put("deptManager",userId);
            map.put("dmerStatus",dmerStatus);
            map.put("page",pageParams);
            List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBs1 = vehicleUsageMapper.deptApproval(map);
            for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:vehicleUsageWithBLOBs1){

                //车牌号
                VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs2.getvId()));
                if(vehicleWithBLOBs!=null){
                    vehicleUsageWithBLOBs2.setvIdNum(vehicleWithBLOBs.getvNum());
                }
                //用车人
                String vuUser = vehicleUsageWithBLOBs2.getVuUser();
                if(!StringUtils.checkNull(vuUser)){
                    Users byuserId = usersMapper.getByUserId(vuUser.substring(0,vuUser.length()-1));
                    if(byuserId!=null){
                        vehicleUsageWithBLOBs2.setVuUserName(byuserId.getUserName());
                    }
                }
                //随行人员处理
                String vuSuite = vehicleUsageWithBLOBs2.getVuSuite();
                if(vuSuite!=null && vuSuite!=""){
                    StringBuffer sb =new StringBuffer();
                    String[] split = vuSuite.split(",");
                    for(String s :split){
                        Users byuserId2 = usersMapper.getByUserId(s);
                        if(byuserId2!=null){
                            sb.append(byuserId2.getUserName()+",");
                        }
                        vehicleUsageWithBLOBs2.setVuSuiteName(sb.toString());
                    }

                }
            }
            ajaxJson.setObj(vehicleUsageWithBLOBs1);
            ajaxJson.setMsg("ok");
            ajaxJson.setFlag(true);
            ajaxJson.setTotleNum(pageParams.getTotal());
        } catch (Exception e) {
            ajaxJson.setFlag(false);
            ajaxJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxJson;
    }

    public AjaxJson editDeptApproval(VehicleUsageWithBLOBs vehicleUsageWithBLOBs,HttpServletRequest request){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            vehicleUsageWithBLOBs.setVuStatus("0");
            int i = vehicleUsageMapper.updateByPrimaryKeySelective(vehicleUsageWithBLOBs);
            SmsBody smsBody = new SmsBody();
            if ("1".equals(vehicleUsageWithBLOBs.getDmerStatus())){
                smsBody.setContent("您的车辆使用申请已被批准");
            }else if ("2".equals(vehicleUsageWithBLOBs.getDmerStatus())){
                smsBody.setContent("您的车辆使用申请未被批准");
            }
            smsBody.setFromId(user.getUserId());
            smsBody.setSmsType("9");
            smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
            smsBody.setRemindUrl("/veHicle/veHicleUseageIndex");
            smsService.saveSms(smsBody, user.getUserId(), "1", "0", "", "", "");

            ajaxJson.setFlag(true);
            ajaxJson.setMsg("ok");
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            e.printStackTrace();
        }
        return ajaxJson;
    }

    public AjaxJson editOperatorstatu(VehicleUsageWithBLOBs vehicleUsageWithBLOBs,HttpServletRequest request){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            int i = vehicleUsageMapper.updateByPrimaryKeySelective(vehicleUsageWithBLOBs);
            SmsBody smsBody = new SmsBody();
            if ("1".equals(vehicleUsageWithBLOBs.getDmerStatus())){
                smsBody.setContent("您的车辆使用申请已被批准");
                //向司机发送事务提醒
                String s = vehicleUsageWithBLOBs.getVuDriver();
                if(!StringUtils.checkNull(s)){
                    SmsBody smsBody2 = new SmsBody();
                    smsBody2.setFromId(user.getUserId());
                    smsBody2.setSmsType("9");
                    smsBody2.setContent("请查看新的出行任务");
                    smsBody2.setSendTime((int)(System.currentTimeMillis() / 1000));
                    smsBody2.setRemindUrl("/veHicle/veHicledetail?vuId="+vehicleUsageWithBLOBs.getVuId());
                    if(vehicleUsageWithBLOBs.getVuDriver()!=null) {
                        smsService.saveSms(smsBody2,s, "1", "0", "", "", "");
                    }
                    if (!StringUtils.checkNull(vehicleUsageWithBLOBs.getSms2Remind())) {
                        smsBody2.setIsAttach("1");
                    }
                }

            }else if ("2".equals(vehicleUsageWithBLOBs.getDmerStatus())){
                smsBody.setContent("您的车辆使用申请未被批准");
            }
            smsBody.setFromId(user.getUserId());
            smsBody.setSmsType("9");
            smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
            smsBody.setRemindUrl("/veHicle/veHicleUseageIndex");

            smsService.saveSms(smsBody, user.getUserId(), "1", "0", "", "", "");

            if(i>0){
                ajaxJson.setFlag(true);
                ajaxJson.setMsg("ok");
            }
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/21 11:49
     * 类介绍  :   车辆管理预定情况
     * 构造参数:
     */
    public ToJson<VehicleUsageWithBLOBs> getReservation(Integer vId){
        ToJson<VehicleUsageWithBLOBs> json =new ToJson<VehicleUsageWithBLOBs>(1,"err");
        try {
            //未准的 已准的 使用中的 待批
            List<VehicleUsageWithBLOBs> reservation = vehicleUsageMapper.getReservation(vId);
            for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:reservation){
                //有部门审批员
                if(vehicleUsageWithBLOBs2.getDeptManager()!=null &&vehicleUsageWithBLOBs2.getDeptManager()!=""){
                    if(vehicleUsageWithBLOBs2.getDmerStatus().equals("0")){
                        vehicleUsageWithBLOBs2.setVuStatusName("部门审批员待审批");
                    }
                    else if(vehicleUsageWithBLOBs2.getDmerStatus().equals("1")){
                        vehicleUsageWithBLOBs2.setVuStatusName("部门审批员已批准");
                    }else{
                        vehicleUsageWithBLOBs2.setVuStatusName("部门审批员未批准");
                    }
                }else{
                    if(vehicleUsageWithBLOBs2.getVuStatus().equals("0")){
                        vehicleUsageWithBLOBs2.setVuStatusName("调度员待审批");
                    }
                    else if(vehicleUsageWithBLOBs2.getVuStatus().equals("1")){
                        vehicleUsageWithBLOBs2.setVuStatusName("调度员已批准");
                    }
                    else{
                        vehicleUsageWithBLOBs2.setVuStatusName("调度员未批准");
                    }
                }
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs:reservation){
                    //车牌号
                    VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs.getvId()));
                    if(vehicleWithBLOBs!=null){
                        vehicleUsageWithBLOBs.setvIdNum(vehicleWithBLOBs.getvNum());
                    }
                    //用车人
                    if(vehicleUsageWithBLOBs.getVuUser()!=null && vehicleUsageWithBLOBs.getVuUser()!=""){
                        Users byuserId = usersMapper.getByUserId(vehicleUsageWithBLOBs.getVuUser().substring(0,vehicleUsageWithBLOBs.getVuUser().length()-1));
                        if(byuserId!=null){
                            vehicleUsageWithBLOBs.setVuUserName(byuserId.getUserName());
                        }
                    }

                    //部门审批人处理
                    if(vehicleUsageWithBLOBs.getDeptManager()!=null && vehicleUsageWithBLOBs.getDeptManager()!=""){
                        Users byuserId1 = usersMapper.getByUserId(vehicleUsageWithBLOBs.getDeptManager().substring(0,vehicleUsageWithBLOBs.getDeptManager().length()-1));
                        if(byuserId1!=null){
                            vehicleUsageWithBLOBs.setDeptManagerName(byuserId1.getUserName());
                        }
                    }

                    //随行人员处理
                    String vuSuite = vehicleUsageWithBLOBs.getVuSuite();
                    if(vuSuite!=null && vuSuite!=""){
                        StringBuffer sb =new StringBuffer();
                        String[] split = vuSuite.split(",");
                        for(String s :split){
                            Users byuserId2 = usersMapper.getByUserId(s);
                            if(byuserId2!=null){
                                sb.append(byuserId2.getUserName()+",");
                            }
                            vehicleUsageWithBLOBs.setVuSuiteName(sb.toString());
                        }

                    }

                }
            }
            json.setObj(reservation);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg("err");
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月19日 下午17:01:41
     * 方法介绍:   调度员审批列表展示
     * 参数说明:   request
     * @return    ajaxjson
     */
    public AjaxJson GetOperator(HttpServletRequest request,String applyStartTime,String applyEndTime,VehicleUsageWithBLOBs vehicleUsageWithBLOBs){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            Map<String,Object> map =new HashMap<String,Object>();
            //从session中获取当前登录人的信息
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            String userId = users.getUserId();
            vehicleUsageWithBLOBs.setVuOperator(userId);
            //调度员待审批
            List<VehicleUsageWithBLOBs>  vehicleUsageWithBLOBsList = null;
            if(vehicleUsageWithBLOBs.getSign().equals("0")){
                vehicleUsageWithBLOBsList=vehicleUsageMapper.GetOperator(vehicleUsageWithBLOBs);
                //查看一下没有设置部门审批员信息
               /* map.put("vuOperator",userId);
                List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllPendingVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs2.getDeptManager()==null || vehicleUsageWithBLOBs2.getDeptManager().equals("")){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs2);
                    }
                }*/
            }
            else if(vehicleUsageWithBLOBs.getSign().equals("1")){
                vehicleUsageWithBLOBsList=vehicleUsageMapper.GetOperator(vehicleUsageWithBLOBs);
                //查看一下没有设置部门审批员信息
               /* Map<String,Object> map1 =new HashMap<String,Object>();
                map1.put("vuOperator",userId);
                List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllapproveVehicle(map1);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs2.getDeptManager()==null  || vehicleUsageWithBLOBs2.getDeptManager().equals("")){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs2);
                    }
                }*/
                //使用中
            }else if(vehicleUsageWithBLOBs.getSign().equals("2")){
                vehicleUsageWithBLOBsList=vehicleUsageMapper.selectAlredyAllVeHicle();
                /*List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList1 = vehicleUsageMapper.getAllApprovalVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:vehicleUsageWithBLOBsList1){
                    if(vehicleUsageWithBLOBs2.getDeptManager()==null|| vehicleUsageWithBLOBs2.getDeptManager().equals("")){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs2);
                    }
                }*/
                //遍历已批准 查看时间
                List <VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList3 =new ArrayList<VehicleUsageWithBLOBs>();
                for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs4:vehicleUsageWithBLOBsList){
                    Date date =new Date();
                    long startTime = 0;
                    long endTime = 0;
                    Date vuStart = vehicleUsageWithBLOBs4.getVuStart();
                    if (vuStart!=null){
                        startTime = vuStart.getTime();
                    }
                    Date vuEnd = vehicleUsageWithBLOBs4.getVuEnd();
                    if (vuEnd!=null){
                        endTime = vuEnd.getTime();
                    }
                    long time = date.getTime();
                    if(startTime<=time&&endTime>=time){
                        vehicleUsageWithBLOBsList3.add(vehicleUsageWithBLOBs4);
                    }
                }
                //给vehicleUsageWithBLOBs1重新赋值成为使用中的车辆
                vehicleUsageWithBLOBsList=vehicleUsageWithBLOBsList3;
            }
            //未准
            else if(vehicleUsageWithBLOBs.getSign().equals("3")){
                //给vehicleUsageWithBLOBs1重新赋值成为未批准车辆
                Map<String,Object> map1 =new HashMap<String,Object>();
                map1.put("vuOperator",userId);
                vehicleUsageWithBLOBsList=vehicleUsageMapper.selectRefuseByOperator(map1);
            }
            //使用结束
            else if(vehicleUsageWithBLOBs.getSign().equals("4")){
                vehicleUsageWithBLOBsList=vehicleUsageMapper.selectAlredyAllVeHicle();
               /* List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList4 = vehicleUsageMapper.getAllApprovalVehicle(map);
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs3:vehicleUsageWithBLOBsList4){
                    if(vehicleUsageWithBLOBs3.getDeptManager()==null|| vehicleUsageWithBLOBs3.getDeptManager().equals("")){
                        vehicleUsageWithBLOBsList.add(vehicleUsageWithBLOBs3);
                    }
                }*/
                //遍历已批准 查看时间
                List <VehicleUsageWithBLOBs> vehicleUsageWithBLOBsList2 =new ArrayList<VehicleUsageWithBLOBs>();
                for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs5:vehicleUsageWithBLOBsList){
                    Date date =new Date();
                    long startTime = 0;
                    long endTime = 0;
                    Date vuStart = vehicleUsageWithBLOBs5.getVuStart();
                    if (vuStart!=null){
                        startTime = vuStart.getTime();
                    }
                    Date vuEnd = vehicleUsageWithBLOBs5.getVuEnd();
                    if (vuEnd!=null){
                        endTime = vuEnd.getTime();
                    }
                    long time = date.getTime();
                    if(startTime<=time && endTime<=time){
                        vehicleUsageWithBLOBsList2.add(vehicleUsageWithBLOBs5);
                    }
                }
                //给vehicleUsageWithBLOBs1重新赋值成为使用结束的车辆
                vehicleUsageWithBLOBsList=vehicleUsageWithBLOBsList2;
            }else{
                vehicleUsageWithBLOBsList=vehicleUsageMapper.GetOperator(vehicleUsageWithBLOBs);
            }

            if(vehicleUsageWithBLOBsList!=null){
                for (VehicleUsageWithBLOBs vehicleUsageWithBLOBs1:vehicleUsageWithBLOBsList){
                    //车牌号
                    VehicleWithBLOBs vehicleWithBLOBs2 = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs1.getvId()));
                    if(vehicleWithBLOBs2!=null){
                        vehicleUsageWithBLOBs1.setvIdNum(vehicleWithBLOBs2.getvNum());
                    }
                    //用车人
                    String vuUser = vehicleUsageWithBLOBs1.getVuUser();
                    if(!StringUtils.checkNull(vuUser)){
                        Users byuserId = usersMapper.getByUserId(vuUser.substring(0,vuUser.length()-1));
                        if(byuserId!=null){
                            vehicleUsageWithBLOBs1.setVuUserName(byuserId.getUserName());
                        }
                    }

                    //申请人

                    String vuProposer = vehicleUsageWithBLOBs1.getVuProposer();
                    if(!StringUtils.checkNull(vuProposer)){
                        Users byuserId3 = usersMapper.getByUserId(vuProposer.substring(0,vuProposer.length()-1));
                        if(byuserId3!=null){
                            vehicleUsageWithBLOBs1.setVuProposerName(byuserId3.getUserName());
                        }
                    }
                    //部门审批人处理
                    String deptManager = vehicleUsageWithBLOBs1.getDeptManager();
                    if(!StringUtils.checkNull(deptManager)){
                        Users byuserId1 = usersMapper.getByUserId(deptManager.substring(0,deptManager.length()-1));
                        if(byuserId1!=null){
                            vehicleUsageWithBLOBs1.setDeptManagerName(byuserId1.getUserName());
                        }
                    }
                    //随行人员处理
                    String vuSuite = vehicleUsageWithBLOBs1.getVuSuite();
                    if(vuSuite!=null && vuSuite!=""){
                        StringBuffer sb =new StringBuffer();
                        String[] split = vuSuite.split(",");
                        for(String s :split){
                            Users byuserId2 = usersMapper.getByUserId(s);
                            if(byuserId2!=null){
                                sb.append(byuserId2.getUserName()+",");
                            }
                            vehicleUsageWithBLOBs1.setVuSuiteName(sb.toString());
                        }

                    }

                }
            }


            ajaxJson.setFlag(true);
            ajaxJson.setMsg("ok");
            ajaxJson.setObj(vehicleUsageWithBLOBsList);
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            e.printStackTrace();
        }
        return  ajaxJson;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月25日 下午13:01:41
     * 方法介绍:   车辆使用详情接口
     * 参数说明:   vuId
     * @return    VehicleUsageWithBLOBs
     */
    public AjaxJson getVehicleUsageDetail(Integer vuId){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            VehicleUsageWithBLOBs vehicleUsageWithBLOBs = vehicleUsageMapper.selectByPrimaryKey(vuId);
            if(vehicleUsageWithBLOBs!=null){
                //车牌号
                VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs.getvId()));
                if(vehicleWithBLOBs!=null){
                    vehicleUsageWithBLOBs.setvIdNum(vehicleWithBLOBs.getvNum());
                }
                //司机
                String s1=vehicleUsageWithBLOBs.getVuDriver();
                if(!StringUtils.checkNull(s1)){
                    Users byuserId6 = usersMapper.getByUserId(s1.replace(",",""));
                    if(byuserId6!=null){
                        vehicleUsageWithBLOBs.setVuDriverName(byuserId6.getUserName());
                    }
                }
                //申请人

                String vuProposer = vehicleUsageWithBLOBs.getVuProposer();
                if(!StringUtils.checkNull(vuProposer)){
                    Users byuserId3 = usersMapper.getByUserId(vuProposer.replace(",",""));
                    if(byuserId3!=null){
                        vehicleUsageWithBLOBs.setVuProposerName(byuserId3.getUserName());
                    }
                }
                //用车部门
                String vuDept = vehicleUsageWithBLOBs.getVuDept();
                if(!StringUtils.checkNull(vuDept)){
                    Department deptById = departmentMapper.getDeptById(Integer.valueOf(vuDept.replace(",","")));
                    if(deptById!=null){
                        vehicleUsageWithBLOBs.setVuDeptName(deptById.getDeptName());
                    }
                }
                //用车人
                String vuUser = vehicleUsageWithBLOBs.getVuUser();
                if(!StringUtils.checkNull(vuUser)){
                    Users byuserId = usersMapper.getByUserId(vuUser.replace(",",""));
                    if(byuserId!=null){
                        vehicleUsageWithBLOBs.setVuUserName(byuserId.getUserName());
                    }
                }


                //调度员
                Users byuserId5 = usersMapper.getByUserId(vehicleUsageWithBLOBs.getVuOperator());
                if(byuserId5!=null){
                    vehicleUsageWithBLOBs.setVuOperatorName(byuserId5.getUserName());
                }

                //部门审批人处理
                String deptManager = vehicleUsageWithBLOBs.getDeptManager();
                if(!StringUtils.checkNull(deptManager)){
                    Users byuserId1 = usersMapper.getByUserId(deptManager.replace(",",""));
                    if(byuserId1!=null){
                        vehicleUsageWithBLOBs.setDeptManagerName(byuserId1.getUserName());
                    }
                }

                //随行人员处理
                String vuSuite = vehicleUsageWithBLOBs.getVuSuite();
                if(vuSuite!=null && vuSuite!=""){
                    StringBuffer sb =new StringBuffer();
                    String[] split = vuSuite.split(",");
                    for(String s :split){
                        Users byuserId2 = usersMapper.getByUserId(s);
                        if(byuserId2!=null){
                            sb.append(byuserId2.getUserName()+",");
                        }
                        vehicleUsageWithBLOBs.setVuSuiteName(sb.toString());
                    }
                }
                ajaxJson.setObj(vehicleUsageWithBLOBs);
                ajaxJson.setFlag(true);
                ajaxJson.setMsg("ok");
            }
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            e.printStackTrace();
        }
        return ajaxJson;

    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月25日 下午13:01:41
     * 方法介绍:   车辆使用删除接口
     * 参数说明:   vuId
     * @return    AjaxJson
     */
    public AjaxJson deleteVehicleUsage(Integer vuId){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            int i = vehicleUsageMapper.deleteByPrimaryKey(vuId);
            if(i>0){
                ajaxJson.setFlag(true);
                ajaxJson.setMsg("ok");
            }
        } catch (Exception e) {
            ajaxJson.setFlag(false);
            ajaxJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月27日 下午13:01:41
     * 方法介绍:   车辆使用编辑接口
     * 参数说明:   vuId
     * @return    AjaxJson
     */
    public AjaxJson editVehicleUsage(VehicleUsageWithBLOBs vehicleUsageWithBLOBs){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            int i = vehicleUsageMapper.updateByPrimaryKeySelective(vehicleUsageWithBLOBs);
            if(i>0){
                ajaxJson.setFlag(true);
                ajaxJson.setMsg("ok");
            }
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 显示车辆申请数据

     */
    public AjaxJson selectAllObjects(HttpServletRequest request, PageParams pageParams){
        AjaxJson ajaxJson =new AjaxJson();
        ajaxJson.setSuccess(true);
        ajaxJson.setFlag(false);
        try {
            //从session中获取当前登录人的信息
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            String userId = users.getUserId();
            VehicleUsageWithBLOBs vehicleUsageWithBLOBs =new VehicleUsageWithBLOBs();
            vehicleUsageWithBLOBs.setDeptManager(userId);
            // vehicleUsageWithBLOBs.setDmerStatus(dmerStatus);
            Map<String,Object> map = new HashMap<>();
            map.put("page",pageParams);
            List<VehicleUsageWithBLOBs> vehicleUsageWithBLOBs1 = vehicleUsageMapper.selectAllObjects(map);
            for(VehicleUsageWithBLOBs vehicleUsageWithBLOBs2:vehicleUsageWithBLOBs1){

                if(!StringUtils.checkNull(vehicleUsageWithBLOBs2.getvId())){
                    //车牌号
                    VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleUsageWithBLOBs2.getvId()));
                    if(vehicleWithBLOBs!=null){
                        vehicleUsageWithBLOBs2.setvIdNum(vehicleWithBLOBs.getvNum());
                    }
                }
                //用车人
                String vuUser = vehicleUsageWithBLOBs2.getVuUser();
                if(!StringUtils.checkNull(vuUser)){
                    Users byuserId = usersMapper.getByUserId(vuUser.replace(",",""));
                    if(byuserId!=null){
                        vehicleUsageWithBLOBs2.setVuUserName(byuserId.getUserName());
                    }
                }
                //随行人员处理
                String vuSuite = vehicleUsageWithBLOBs2.getVuSuite();
                if(vuSuite!=null && vuSuite!=""){
                    StringBuffer sb =new StringBuffer();
                    String[] split = vuSuite.split(",");
                    for(String s :split){
                        Users byuserId2 = usersMapper.getByUserId(s);
                        if(byuserId2!=null){
                            sb.append(byuserId2.getUserName()+",");
                        }
                        vehicleUsageWithBLOBs2.setVuSuiteName(sb.toString());
                    }

                }
            }

            ajaxJson.setObj(vehicleUsageWithBLOBs1);
            ajaxJson.setMsg("ok");
            ajaxJson.setFlag(true);
            if(pageParams!=null){
                ajaxJson.setTotleNum(pageParams.getTotal());
            }
        } catch (Exception e) {
            ajaxJson.setFlag(false);
            ajaxJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxJson;
    }

    //事务提醒
    @Async
    public void VehicleUsageReminder(HttpServletRequest request, final VehicleUsageWithBLOBs vehicleUsageWithBLOBs , final String todoId){
        final String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
        final Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        this.threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                ContextHolder.setConsumerType("xoa" + sqlType);
                SmsBody smsBody = new SmsBody();
                smsBody.setFromId(vehicleUsageWithBLOBs.getVuProposer());
                smsBody.setContent("车辆申请！主题：" + vehicleUsageWithBLOBs.getRemindContent());
                Date date = new Date();
                long dateLong = date.getTime()/1000;
                String strDate = String.valueOf(dateLong);
                Integer dateInt = Integer.valueOf(strDate.trim());
                smsBody.setSendTime(dateInt);
                smsBody.setIsAttach("0"); //无附件
                smsBody.setRemindUrl("/veHicle/veHicledetail?vuId="+vehicleUsageWithBLOBs.getVuId());

                SysCode sysCode = new SysCode();
                sysCode.setCodeName("车辆申请");
                sysCode.setParentNo("SMS_REMIND");
                SysCode codeNoByNameAndParentNo = sysCodeMapper.getCodeNoByNameAndParentNo(sysCode);
                if (codeNoByNameAndParentNo != null) {
                    smsBody.setSmsType(codeNoByNameAndParentNo.getCodeNo());
                }
                String title = user.getUserName() + "：有提醒！";
                String context = "主题:" + vehicleUsageWithBLOBs.getRemindContent();
                smsService.saveSms(smsBody, todoId, "1", "1", title, context, sqlType);
            }
        });
    }


}
